Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(suite-native): add trading slice to mobile #17120

Merged
merged 1 commit into from
Feb 21, 2025

Conversation

vytick
Copy link
Contributor

@vytick vytick commented Feb 19, 2025

Adds trading slice with tradingReducer to mobile app

@vytick vytick added mobile Suite Lite issues and PRs no-project This label is used to specify that PR doesn't need to be added to a project labels Feb 19, 2025
@vytick vytick requested a review from a team as a code owner February 19, 2025 21:25
@vytick vytick requested a review from jbazant February 19, 2025 21:25
Copy link

coderabbitai bot commented Feb 19, 2025

Walkthrough

The pull request introduces several updates to trading-related modules. In the suite-common/trading package, a new export for the type TradingBuyState is added from the buyReducer. In the suite-native/module-trading, a new export statement includes all entities from the tradingSlice, which defines new interfaces for managing trading state, an initial state, a Redux slice with reducers and actions, and memoized selectors. The tradingSlice reducer is incorporated into the state reducers under a new trading key. Additionally, the tradingMiddleware is added to the Redux store configuration. Enhancements to the Dispatch interface in the Redux module include new overloads for handling asynchronous thunk actions, improving type safety and flexibility. The changes collectively expand the set of exported entities and enhance state management capabilities specific to trading without altering any existing functionality.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 15eb648 and 0ed7563.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (10)
  • suite-common/trading/src/index.ts (1 hunks)
  • suite-native/module-trading/src/__tests__/tradingSlice.test.ts (1 hunks)
  • suite-native/module-trading/src/index.ts (1 hunks)
  • suite-native/module-trading/src/redux.d.ts (1 hunks)
  • suite-native/module-trading/src/tradingSlice.ts (1 hunks)
  • suite-native/state/package.json (2 hunks)
  • suite-native/state/redux.d.ts (1 hunks)
  • suite-native/state/src/reducers.ts (3 hunks)
  • suite-native/state/src/store.ts (2 hunks)
  • suite-native/state/tsconfig.json (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (10)
  • suite-native/module-trading/src/index.ts
  • suite-native/state/tsconfig.json
  • suite-native/state/src/store.ts
  • suite-common/trading/src/index.ts
  • suite-native/state/src/reducers.ts
  • suite-native/state/package.json
  • suite-native/module-trading/src/redux.d.ts
  • suite-native/module-trading/src/tests/tradingSlice.test.ts
  • suite-native/state/redux.d.ts
  • suite-native/module-trading/src/tradingSlice.ts
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: PR-check / web-authorizeCoinjoin cancelCoinjoinAuthorization passphrase unlockPath setBusy checkFirmwareAuthenticity keepSession cancel.test info.test resetDevice-api
  • GitHub Check: build-deploy
  • GitHub Check: build-deploy
  • GitHub Check: prepare_android_test_app
  • GitHub Check: EAS Update
  • GitHub Check: transport-e2e-test
  • GitHub Check: build-web
  • GitHub Check: Analyze with CodeQL (javascript)
  • GitHub Check: build-web
  • GitHub Check: test
  • GitHub Check: Setup and Cache Dependencies

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
suite-native/module-trading/src/tradingSlice.ts (3)

13-25: Consider adding JSDoc comments for type definitions.

The type definitions are well-structured, but adding JSDoc comments would improve code documentation and IDE support.

Add documentation for the interfaces and types:

+/**
+ * Extends CommonTradingBuyState to include mobile-specific trading functionality
+ */
 export interface TradingBuyState extends CommonTradingBuyState {
     selectedReceiveAccount: ReceiveAccount | undefined;
 }

+/**
+ * Represents the complete trading state including buy operations
+ */
 interface TradingState extends CommonTradingState {
     buy: TradingBuyState;
 }

+/**
+ * Represents the root state structure for trading functionality
+ */
 export type TradeRootState = {
     wallet: {
         trading: TradingState;
     };
 };

32-51: Consider adding error handling for invalid receive accounts.

The reducer should validate the receive account before setting it in the state.

Add validation in the reducer:

 setBuySelectedReceiveAccount: (
     state,
     { payload }: PayloadAction<{ selectedReceiveAccount: ReceiveAccount }>,
 ) => {
+    // Validate receive account before setting
+    if (!payload.selectedReceiveAccount?.address) {
+        console.warn('Invalid receive account provided');
+        return;
+    }
     state.buy.selectedReceiveAccount = payload.selectedReceiveAccount;
 },

55-60: Consider memoizing complex selectors.

While the current selectors are simple, consider using createSelector for any future complex selectors that compute derived data.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 032177a and 15b92da.

📒 Files selected for processing (5)
  • suite-common/trading/src/index.ts (1 hunks)
  • suite-native/module-trading/src/index.ts (1 hunks)
  • suite-native/module-trading/src/tradingSlice.ts (1 hunks)
  • suite-native/state/src/reducers.ts (3 hunks)
  • suite-native/state/src/store.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (19)
  • GitHub Check: e2e-test-suite-web (@group_wallet, trezor-user-env-unix bitcoin-regtest, 1)
  • GitHub Check: e2e-test-suite-web (@group_device-management, trezor-user-env-unix, 1)
  • GitHub Check: e2e-test-suite-web (@group_suite, trezor-user-env-unix, 1)
  • GitHub Check: e2e-test-suite-web (@group=other, trezor-user-env-unix)
  • GitHub Check: e2e-test-suite-web (@group=metadata2, trezor-user-env-unix)
  • GitHub Check: e2e-test-suite-web (@group=metadata1, trezor-user-env-unix)
  • GitHub Check: e2e-test-suite-web (@group=settings, trezor-user-env-unix)
  • GitHub Check: e2e-test-suite-web (@group=suite, trezor-user-env-unix)
  • GitHub Check: Unit Tests
  • GitHub Check: Type Checking
  • GitHub Check: Linting and formatting
  • GitHub Check: run-desktop-tests (@group=wallet, trezor-user-env-unix bitcoin-regtest)
  • GitHub Check: run-desktop-tests (@group=other, trezor-user-env-unix)
  • GitHub Check: run-desktop-tests (@group=passphrase, trezor-user-env-unix)
  • GitHub Check: run-desktop-tests (@group=settings, trezor-user-env-unix bitcoin-regtest)
  • GitHub Check: run-desktop-tests (@group=device-management, trezor-user-env-unix)
  • GitHub Check: Analyze with CodeQL (javascript)
  • GitHub Check: prepare_android_test_app
  • GitHub Check: run-desktop-tests (@group=suite, trezor-user-env-unix)
🔇 Additional comments (7)
suite-native/module-trading/src/index.ts (1)

2-2: LGTM!

The export makes all trading slice entities available to other modules, which aligns with the PR objective of adding trading functionality to mobile.

suite-common/trading/src/index.ts (1)

7-7: LGTM!

The export of TradingBuyState type from the buy reducer aligns with the trading slice implementation.

suite-native/module-trading/src/tradingSlice.ts (1)

27-30: LGTM!

The initial state correctly extends the common initial state with mobile-specific trading functionality.

suite-native/state/src/store.ts (1)

29-29: LGTM!

The trading middleware is correctly integrated into the Redux store configuration.

suite-native/state/src/reducers.ts (3)

32-32: LGTM!

The import statement follows the established pattern and is correctly placed among other feature module imports.


87-87: LGTM!

The trading reducer is correctly integrated into the wallet reducers, making the trading state accessible under the 'wallet.trading' path.


63-63:

❓ Verification inconclusive

Verify persistence requirements for trading state.

The trading reducer is correctly prepared, but unlike some other reducers (e.g., accounts, transactions), it's not configured for persistence. Please confirm if this is intentional.


🏁 Script executed:

#!/bin/bash
# Description: Check if other trading-related files mention persistence requirements

# Search for persistence-related configurations in trading module
rg -i "persist|storage" --type ts --glob "suite-native/module-trading/**"

Length of output: 75


Action Required: Confirm Trading State Persistence

The trading reducer appears to be prepared correctly using tradingSlice.prepareReducer(extraDependencies). However, unlike other reducers (e.g., those managing accounts or transactions), it lacks any explicit persistence configuration. Our search in the suite-native/module-trading directory did not reveal any persistence-related settings. Please verify manually whether the omission of persistence for the trading state is intentional or if it requires alignment with the persistence patterns in other parts of the codebase.

Copy link
Contributor

@PeKne PeKne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not test it, just looked into the code. LGTM

Copy link
Contributor

@jbazant jbazant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I have just one note. Reducer is a logic that is IMO most simple to test and I even like to use it as good example where TDD can be applied, because responsibility and expected results are clear.

I usually start like:

it('should have correct initial state', () => {
  const state = myReducer(undefined, {action: 'unknown'});

  expect(state).toEqual(expectedInitialState);
});

describe('myAction1', () => {
  it('should set X', () => {
    const state = myReucer(undefined, myAction1());

    expect(state).toEqual(expect.objectContaining({ x: true }));
  });

  describe('selectMyX', /* ...*/);
});

Would it be possible to add some basic test coverage please?

coderabbitai[bot]

This comment was marked as off-topic.

@trezor trezor deleted a comment from coderabbitai bot Feb 20, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
suite-native/module-trading/src/tradingSlice.ts (1)

46-47: Improve comment clarity.

The current comment could be more descriptive about the fallback behavior.

-            // In case that this reducer does not match the action, try to handle it by suite-common tradingReducer.
+            // Fallback to suite-common tradingReducer for unhandled actions to maintain compatibility with common trading functionality.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a6ff60f and 7d40bc2.

📒 Files selected for processing (2)
  • suite-native/module-trading/src/tradingSlice.ts (1 hunks)
  • suite-native/state/src/reducers.ts (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • suite-native/state/src/reducers.ts
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: EAS Update
  • GitHub Check: build-web
  • GitHub Check: prepare_android_test_app
  • GitHub Check: build-web
  • GitHub Check: Analyze with CodeQL (javascript)
  • GitHub Check: Setup and Cache Dependencies
🔇 Additional comments (6)
suite-native/module-trading/src/tradingSlice.ts (6)

1-12: LGTM! Well-organized imports with proper scoping.

The imports are properly organized, with clear separation between external dependencies and internal modules.


13-25: LGTM! Well-structured type definitions.

The interfaces and types are well-defined with proper extension of common types, providing good type safety and clear state structure.


27-30: LGTM! Proper state initialization.

The initial state is correctly defined, preserving common state while extending with new fields.


32-42: LGTM! Well-structured Redux slice.

The slice is well-implemented with proper state mutations using Redux Toolkit's Immer integration.


53-53: LGTM! Proper action export.

The action creator is correctly exported following Redux best practices.


59-61: Consider renaming selector and simplifying implementation.

Based on the past review comments, selectors should not be prefixed with "select" unless they are actual selectors. Additionally, the implementation could be simplified.

-export const selectBuySelectedReceiveAccount = (state: TradeRootState) =>
-    selectTradingBuy(state).selectedReceiveAccount;
+export const buyReceiveAccount = (state: TradeRootState) => selectTradingBuy(state).selectedReceiveAccount;

Copy link

github-actions bot commented Feb 20, 2025

🚀 Expo preview is ready!

  • Project → trezor-suite-preview
  • Platforms → android, ios
  • Scheme → trezorsuitelite
  • Runtime Version → 26
  • More info

Learn more about 𝝠 Expo Github Action

@trezor-ci trezor-ci force-pushed the feat/native/introduce-trade-reducer branch from 0d0dce5 to 8e6871f Compare February 21, 2025 07:41
coderabbitai[bot]

This comment was marked as off-topic.

@trezor trezor deleted a comment from github-actions bot Feb 21, 2025
Copy link
Contributor

@jbazant jbazant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@vytick
Copy link
Contributor Author

vytick commented Feb 21, 2025

/rebase

Copy link

@trezor-ci trezor-ci force-pushed the feat/native/introduce-trade-reducer branch from f392bd9 to 15eb648 Compare February 21, 2025 10:36
@vytick vytick enabled auto-merge (rebase) February 21, 2025 11:02
@vytick vytick force-pushed the feat/native/introduce-trade-reducer branch from 15eb648 to 0ed7563 Compare February 21, 2025 11:03
@vytick vytick merged commit aadf4cc into develop Feb 21, 2025
42 checks passed
@vytick vytick deleted the feat/native/introduce-trade-reducer branch February 21, 2025 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mobile Suite Lite issues and PRs no-project This label is used to specify that PR doesn't need to be added to a project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants